{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The model below is converted from tensorflow. The original model can be found [here](https://github.com/hunglc007/tensorflow-yolov4-tflite). The conversion process follows the basic outline provided by the [tensorflow-onnx repo](https://github.com/onnx/tensorflow-onnx)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1: Cloning the repository"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/hunglc007/tensorflow-yolov4-tflite\n",
    "!cd tensorflow-yolov4-tflite"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Downloading the weights\n",
    "Download yolov4.weights file: https://drive.google.com/open?id=1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT\n",
    "\n",
    "Place the downloaded weights in the tensorflow-yolov4-tflite/data/folder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Saving the tf model in preparation for ONNX conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!python save_model.py --weights ./data/yolov4.weights --output ./checkpoints/yolov4.tf --input_size 416 --model yolov4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Conversion\n",
    "1. Follow the installation process listed on the tensorflow-onnx repo, which can be found [here](https://github.com/onnx/tensorflow-onnx#prerequisites).\n",
    "2. Run `tf2onnx.convert`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# installations\n",
    "!pip install onnxruntime\n",
    "!pip install git+https://github.com/onnx/tensorflow-onnx\n",
    "    \n",
    "# Conversion\n",
    "python -m tf2onnx.convert --saved-model ./checkpoints/yolov4.tf --output model.onnx --opset 11 --verbose "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Validation\n",
    "Validation outputs can be found in the [onnx-model-validation](./onnx-model-validation.ipynb) notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import onnx\n",
    "import os\n",
    "\n",
    "# Load the ONNX model\n",
    "model = onnx.load(os.path.join('model.onnx'))\n",
    "onnx.checker.check_model(model)  # Check that the IR is well formed\n",
    "print(onnx.helper.printable_graph(model.graph))  # Print a human readable representation of the graph"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
